/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.app.appuser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cos.controller.base.BaseController;
import com.cos.service.system.appuser.AppuserService;
import com.cos.service.system.user.UserService;
import com.cos.util.AppUtil;
import com.cos.util.Const;
import com.cos.util.PageData;
import com.cos.util.SmsUtil;
import com.cos.util.Tools;

/**
 * 发送短信验证码接口
 * 1.发送短信
 * <p/>
 * 
 * @author <a href="mailto:978954852@qq.com">xfjin</a>
 * @version  Date: 2016年3月25日 下午3:35:43
 * @serial 1.0
 * @since 2016年3月25日 下午3:35:43
 */
@Controller
@RequestMapping(value="/appuser")
public class IntSendSms extends BaseController {
	
	@Resource(name="userService")
	private UserService userService;	
	@Resource(name="appuserService")
	private AppuserService appuserService;
	 
	/**
	 * 发送短信
	 * @throws Exception 
	 */
	@RequestMapping(value="/sendSms")
	@ResponseBody
	public Object sendSms() throws Exception{
		PageData pd = new PageData();
		String code = new String();
		pd = this.getPageData();
		Map<String,Object> map = new HashMap<String,Object>();
		String msg = "ok";		//发送状态
		
		List<PageData> pdList = new ArrayList<PageData>();
		for(int i=0; i<5; i++){
			code += randomChar();
		}
		String codeContent = new String("【"+code+"】");
		String ID = this.get32UUID();
		pd.put("CODE_ID", ID);
		pd.put("CODE", code);
		appuserService.insertToPhoneCode(pd);
		
		String m = new String("您的验证码是："+codeContent+"。请不要把验证码泄露给其他人。");
		pd.remove("CONTENT");
		pd.put("CONTENT", m);
		String PHONEs = pd.getString("PHONE");					//对方邮箱
		String CONTENT = pd.getString("CONTENT");				//内容
		String TYPE = new String("2");						//类型 1：短信接口1   2：短信接口2
			PHONEs = PHONEs.replaceAll(";", ";");
			PHONEs = PHONEs.replaceAll(" ", "");
			String[] arrTITLE = PHONEs.split(";");
			try {
				for(int i=0;i<arrTITLE.length;i++){
					if(Tools.checkMobileNumber(arrTITLE[i])){			//手机号式不对就跳过
						if("1".equals(TYPE)){
							//SmsUtil.sendSms1(arrTITLE[i], CONTENT);		//调用发短信函数1
						}else{
							SmsUtil.sendSms2(arrTITLE[i], CONTENT);		//调用发短信函数2
						}
					}else{
						continue;
					}
				}
				msg = "ok";
			} catch (Exception e) {
				msg = "error";
			} 
			
	//	}	
		pd.put("ID", ID);	
		pd.put("msg", msg);
		pdList.add(pd);
		map.put("list", pdList);
		return AppUtil.returnObject(pd, map);
	}
	
	
	/**
	 * 生成随机数
	 * @return
	 */
	private char randomChar(){
		Random r = new Random();
		String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";
		return s.charAt(r.nextInt(s.length()));
	}
}
